import logging

from tinyops.config import Config
from tinyops.library.notifier import (
    SMTPNotifier,
    ServerChanNotifier,
    UnionNotifier,
)


LOG = logging.getLogger(__name__)


class NotifierManager:
    def __init__(self, config: Config):
        self.config = config
        self.notifier_classes = {
            'server_chan': ServerChanNotifier,
            'smtp': SMTPNotifier,
        }

    def create(self, *notifier_names) -> UnionNotifier:
        LOG.info(f'create notifiers {",".join(notifier_names)}')
        notifiers = []
        for name in notifier_names:
            notifier_config = self.config.notifier(name)
            notifier_type = notifier_config.type
            if notifier_type not in self.notifier_classes:
                raise ValueError(f'not support notifier type {notifier_type}')
            notifier = self.notifier_classes[notifier_type](notifier_config)
            notifiers.append(notifier)
        return UnionNotifier(notifiers)
